$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/barticleinfo/queryList',
        datatype: "json",
        colModel: [
			{ label: 'id', name: 'id', index: 'id',align: "center", width: 50, key: true},
			{ label: '文章名称', name: 'articleTitle', index: 'article_title',align: "center", width: 80,formatter:function(value,options,rowData){
				var id=rowData['id'];
				return "<a onclick=vm.openArticle("+id+")>"+value+"</a>";
				}
			},
			{ label: '创建时间', name: 'createTime', index: 'create_time',align: "center", width: 80 },
			{ label: '文章类型', name: 'articleTypeName', index: 'articleTypeName',align: "center", width: 80 },
			{ label: '创建者', name: 'userName', index: 'userName',align: "center", width: 80 },
			{ label: '分享', name: 'articleShareCount',dataType: "int", sortable: true, index: 'article_share_count',align: "center", width: 80 },
			{ label: '查看', name: 'articleInReadCount',dataType: "int", sortable: true, index: 'article_in_read_count',align: "center", width: 80 },
			{ label: '热度', name: 'articleHotScore',dataType: "int", sortable: true, index: 'article_hot_score',align: "center", width: 80 },
			{ label: '热门', name: 'articleRecommendStatus', index: 'articleRecommendStatus',align: "center", width: 80 ,formatter:function(value,options,rowData){
					if( value===0 || value=="" || value==null){
						return '否';
					}else if(value==1){
						return "<strong style='color: red'>"+'是'+"</>"
					}
				}},
			{ label: '文章来源', name: 'articleSource', index: 'article_source',align: "center", width: 80 ,formatter:function(value,options,rowData){
					var thirdSource=rowData["thirdArticleSource"];
					var source=rowData["articleSource"];
					var content="";
					if(source==1&&thirdSource==1){
						content= "好文";
					}else if(source==1&&thirdSource==2){
						content= "V名片"
					}else if(source==1&&thirdSource!=null&&thirdSource.length>1){
						content= "微信文章";
					}else if( value===1 ){
						content= '平台';
					}else if(value==2){
						content= '公众号';
					}else if(value==3){
						content= '用户'
					}
					return content;
				}
			},
			{ label: '文章状态', name: 'articleStatus', index: 'article_status',align: "center", width: 80 ,formatter:function(value,options,rowData){
					var content="";
					if( value===1 ){
						content= '正常';
					}else if(value==0){
						content= "<p style='color: red'>"+'删除'+"</>"
					}else if(value==2){
						content= '隐藏';
					}
					return content;
				}
			},
			{ label: '操作', name: '', index: '',align: "center", width: 80,formatter:function(value,options,rowData){
					var status=rowData["articleStatus"];
					var id=rowData["id"];
					var content="";
					if( status===1 ){
						content= "<a class='btn btn-primary' onclick=vm.updateStatus('"+id+"',0)>"+"删除"+"</a>";
					}else if(status==0||status==2){
						content= "<a class='btn btn-primary' onclick=vm.updateStatus('"+id+"',1)>"+"恢复"+"</a>";
					}
					return content;
				}
			}
			/*{ label: '操作', name: 'articleStatus', index: 'articleStatus',align: "center", width: 80,sortable: false, editable: false, formatter: alarmFormatter }*/
        ],
		viewrecords: true,
		height: "100%",
		rowNum: 50,
		rowList : [12,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
		postData:{'articleTitle':vm.q.articleTitle,'articleTypeName': vm.q.articleTypeName,"brandId":vm.q.brandId,"articleRecommendStatus":vm.q.articleRecommendStatus},
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
});
setTimeout(function () {
	$.ajax({
		type: "POST",
		async:false,
		url: baseURL + 'sys/bbrandinfo/queryAll',
		contentType: "application/json",
		success: function(r){
			if(r.code==0){
				//vm.bbrandinfo=r.brandInfo;
				for(var i=0;i<r.brandInfo.length;i++){
					$("#brandName").append(
						"<option value=" + r.brandInfo[i].brandName +">"+"</option>");
				}
				$("#brandNames").scrollTop()
			}
		}
	});
},2000)
/*function alarmFormatter(cellvalue, options, rowdata) {
	if(cellvalue==0){
		return "<button class='btn btn-primary ' onclick='updateStatus(" + cellvalue + ")'>恢复</button>";
	}else if(cellvalue==1) {
		return "<button class='btn btn-primary ' @click='updateStatus(" + cellvalue + ")'>删除</button><button class='btn btn-primary ' @click='updateStatus(" + cellvalue + ")'>隐藏</button>";
	}else if(cellvalue==2){
		return "<button class='btn btn-primary ' @click='updateStatus(" + cellvalue + ")'>删除</button><button class='btn btn-primary ' @click='updateStatus(" + cellvalue + ")'>显示</button>";
	}
}*/
var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			articleTypeName:"",
			articleTitle:"",
			brandId:"",
			articleRecommendStatus:1
		},
		showList: true,
		title: null,
		bArticleInfo: {},
		bbrandinfo:{},
		articleTypeName:null,
		articleTitle:null,
		barticletypeinfo:{},
		pp:'pp',
		lx:'lx'
	},
	methods: {
		query: function () {
			vm.q.brandId="";
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.bArticleInfo = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		updateStatus: function (ids,event) {
			var id = ids;
			vm.title = "修改";
			vm.bArticleInfo.id=id;
			console.log('jQuery版本：' + $.fn.jquery);
			if(event==3 || event=="3"){
				vm.bArticleInfo.articleRecommendStatus=1;
			}else{
				vm.bArticleInfo.articleStatus=event;
			}
			confirm('确定修改状态？', function(){
				var url = "sys/barticleinfo/update";
				$.ajax({
					type: "POST",
					url: baseURL + url,
					contentType: "application/json",
					data: JSON.stringify(vm.bArticleInfo),
					success: function(r){
						if(r.code === 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
								//window.location.href="../../../templates/modules/sys/buserinfo.html";
								// window.location.href=baseURL+"sys/permissions/index/"+userId;
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		saveOrUpdate: function (event) {
			var url = vm.bArticleInfo.id == null ? "sys/barticleinfo/save" : "sys/barticleinfo/update";
			var selectValue = $(".type option:first").val();
			if(selectValue=='pp'){
				vm.bArticleInfo.articleBrandId=$('.type option:selected').val();
			}else if(selectValue=='lx') {
				vm.bArticleInfo.articleTypeId = $('.type option:selected').val();
			}
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.bArticleInfo),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/barticleinfo/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$("#type").empty();
			$.get(baseURL + "sys/barticleinfo/info/"+id, function(r){
                vm.bArticleInfo = r.bArticleInfo;
                if(r.type==1) {
					$.get(baseURL + "sys/barticletypeinfo/list/", function (r) {
						vm.barticletypeinfo = r.page.list;
						$("#type").append(
							"<option value=" + vm.lx + ">" + '请选择分类' + "</option>");
						for (i = 0; i < vm.barticletypeinfo.length; i++) {
							$("#type").append(
								"<option value=" + vm.barticletypeinfo[i].id + ">" + vm.barticletypeinfo[i].articleTypeName + "</option>");
							$("#type").find("option[value='" + vm.bArticleInfo.articleTypeId + "']").attr("selected", true);
						}
					});
				}else{
					$.get(baseURL + "sys/bbrandinfo/list/", function (r) {
						vm.barticletypeinfo = r.page.list;
						$("#type").append(
							"<option value=" + vm.pp + ">" + '请选择品牌' + "</option>");
						for (i = 0; i < vm.barticletypeinfo.length; i++) {
							$("#type").append(
								"<option value=" + vm.barticletypeinfo[i].id + ">" + vm.barticletypeinfo[i].brandName + "</option>");
							$("#type").find("option[value='" + vm.bArticleInfo.articleBrandId + "']").attr("selected", true);
						}
					});
				}
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = 1;
			vm.q.articleTypeName=$("#brandNames").val();
			$("#jqGrid").jqGrid('setGridParam',{
				url: baseURL + 'sys/barticleinfo/queryList',
				postData:{'articleTitle':vm.q.articleTitle,'articleTypeName': vm.q.articleTypeName,"brandId":vm.q.brandId,"articleRecommendStatus":vm.q.articleRecommendStatus},
                page:page
            }).trigger("reloadGrid");
		},
		openArticle:function (e) {
			layer.open({
				type: 2,
				title: '文章信息',
				maxmin: true,
				shadeClose: true, //点击遮罩关闭层
				area : ['420px' , '650px'],
				content: "test.html?id="+e
			});
		},
	},
	created:function () {

	}
});